<template>
  <div class="side-menu" :class="{ 'collapsed': isCollapsed }">
    <!-- 添加一个独立的展开按钮，在收缩状态时显示 -->
    <div v-if="isCollapsed" class="expand-button" @click="toggleCollapse">
      <img :src="expandIcon" class="control-icon" alt="展开"/>
    </div>
    
    <!-- 展开状态下只显示图标 -->
    <div v-show="!isCollapsed">
      <div class="menu-items">
        <!-- 将收起按钮移到这里 -->
        <div class="collapse-control" @click.stop="toggleCollapse">
          <img :src="collapseIcon" class="control-icon" alt="收起"/>
        </div>
        
        <div class="menu-item" v-for="item in menuItems" :key="item.path" @click="item.handler">
          <img :src="item.icon" class="menu-icon" :alt="item.name"/>
          <span class="menu-tooltip">{{ item.name }}</span>
          <div v-if="item.hasDropdown" class="settings-dropdown" @click.stop>
            <div class="dropdown-content">
              <div 
                v-for="dropdownItem in item.dropdownItems" 
                :key="dropdownItem.name"
                class="dropdown-item" 
                @click="dropdownItem.handler"
              >
                <span>{{ dropdownItem.name }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <!-- 添加 Profile 对话框组件 -->
  <el-dialog
    v-model="profileDialogVisible"
    :title="'我的账户'"
    width="50%"
    :close-on-click-modal="false"
  >
    <Profile />
  </el-dialog>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'
import { useUserStore } from '@/store/user'
import { ElMessage } from 'element-plus'
import { ref } from 'vue'
import expandIcon from '@/assets/icon/expand.png'
import collapseIcon from '@/assets/icon/collapse.png'
import discoverIcon from '@/assets/icon/discover.png'
import chatIcon from '@/assets/icon/chat.png'

import publishIcon from '@/assets/icon/publish.png'
import bellIcon from '@/assets/icon/bell.png'
import settingIcon from '@/assets/icon/usetting.png'
import moreIcon from '@/assets/icon/more.png'
import virtIcon from '@/assets/icon/virt-man.png'
import assistantIcon from '@/assets/icon/assistant.png'
import ragIcon from '@/assets/icon/rag.png'
import resouceIcon from '@/assets/icon/resource.png'

import Profile from '@/components/Profile.vue'  // 导入 Profile 组件
import { ElMessageBox } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()

const isCollapsed = ref(false)
const toggleCollapse = () => {
  isCollapsed.value = !isCollapsed.value
}

// 将菜单项配置化
const menuItems = [
  { name: '发现', icon: discoverIcon, handler: () => handleDiscover(), path: '/' },
  // { name: '沟通', icon: chatIcon, handler: () => handleChat(), path: '/chat' },
  { 
    name: '智能体', 
    icon: virtIcon, 
    handler: () => {}, // 移除直接跳转
    path: '/virtman',
    hasDropdown: true, // 添加下拉菜单标识
    dropdownItems: [
      { name: '虚拟人', handler: () => router.push('/virtman') },
      { name: '助手', handler: () => router.push('/assistant') },
      // { name: '工作流', handler: () => router.push('/workflow') },
    ]
  },
  { name: '知识库', icon: ragIcon, handler: () => handleGranary(), path: '/granary' },
  { name: '资源管理', icon: resouceIcon, handler: () => handleResource(), path: '/resource' },
  { 
    name: '设置', 
    icon: settingIcon, 
    handler: () => {}, // 移除直接跳转
    path: '/setting',
    hasDropdown: true,
    dropdownItems: [
      { name: userStore.userInfo?.nickname || '未登录', handler: () => showProfileDialog() },
      { name: '设置', handler: () => handleSettingClick() },
      { name: '退出登录', handler: () => handleSignOut() }
    ]
  },
  { name: '更多', icon: moreIcon, handler: () => handleMore(), path: '/more' },
]

const handleDiscover = () => {
  router.push('/')
}

const handleAssistant = () => {
  router.push('/assistant')
}

const handleVirtman = () => {
  router.push('/virtman')
}

const handleGranary = () => {
  router.push('/granary')
}

const handleResource = () => {
  router.push('/resource')
}

const handleChat = () => {
  router.push('/chat')  
}

const handlePublish = () => {
  if (!userStore.token) {
    ElMessage.warning('请先登录')
    router.push('/signin')
    return
  }
  router.push('/publish')
}

const handleNotification = () => {
  if (!userStore.token) {
    ElMessage.warning('请先登录')
    return
  }
  router.push('/notification')
}

const handleSetting = () => {
  // if (!userStore.token) {
  //   ElMessage.warning('请先登录')
  //   return
  // }
  // router.push('/setting')
}

const handleMore = () => {
  router.push('/more')
}

const profileDialogVisible = ref(false)

const showProfileDialog = () => {
  if (!userStore.token) {
    ElMessage.warning('请先登录')
    return
  }
  profileDialogVisible.value = true
}

const handleSettingClick = () => {
  if (!userStore.token) {
    ElMessage.warning('请先登录')
    return
  }
  router.push('/setting')
}

const handleSignOut = () => {
  userStore.singout()
  router.push('/signin')
}

</script>

<style scoped lang="scss">
.side-menu {
  position: fixed;
  width: 60px;
  background: white;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  height: auto;
  padding: 12px 0;
  transition: all 0.3s ease;
  border-radius: 0 12px 12px 0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  
  &.collapsed {
    width: 24px;
    padding: 0;
    background: transparent;
    
    .expand-button {
      position: fixed;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      width: 24px;
      height: 24px;
      background: white;
      border-radius: 0 12px 12px 0;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      
      .control-icon {
        width: 16px;
        height: 16px;
      }
      
      &:hover {
        background-color: #f5f5f5;
      }
    }
  }

  .collapse-control {
    position: absolute;
    right: -12px;
    top: 50%;
    transform: translateY(-50%);
    width: 24px;
    height: 24px;
    background: white;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease, visibility 0.3s ease;
    z-index: 1;
    
    &:hover {
      background-color: #f5f5f5;
    }
    
    .control-icon {
      width: 16px;
      height: 16px;
    }
  }

  &:hover .collapse-control {
    opacity: 1;
    visibility: visible;
  }

  .menu-items {
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  .menu-item, .more-btn {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 12px 20px;
    cursor: pointer;
    
    .menu-icon {
      width: 20px;
      height: 20px;
    }
    
    .menu-tooltip {
      position: absolute;
      left: 100%;
      top: 50%;
      transform: translateY(-50%);
      background: rgba(0, 0, 0, 0.75);
      color: white;
      padding: 4px 8px;
      border-radius: 4px;
      font-size: 12px;
      white-space: nowrap;
      opacity: 0;
      visibility: hidden;
      transition: all 0.3s ease;
      margin-left: 8px;
    }
    
    &:hover {
      background-color: #f5f5f5;
      
      .menu-tooltip {
        opacity: 1;
        visibility: visible;
      }
    }
  }

  .menu-item {
    position: relative;
    
    &:hover {
      .settings-dropdown {
        display: block;
      }
    }
  }

  .settings-dropdown {
    display: none;
    position: absolute;
    left: 100%;
    top: 50%;
    transform: translateY(-50%);
    margin-left: 8px;
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
    padding: 8px 0;
    z-index: 1000;
    min-width: 140px;

    .dropdown-content {
      display: flex;
      flex-direction: column;
    }

    .dropdown-item {
      padding: 8px 16px;
      cursor: pointer;
      font-size: 14px;
      color: #333;
      
      &:hover {
        background-color: #f5f5f5;
      }
    }
  }
}
</style>